import {
	sub,
	pub
} from '@/common/event.js'
import {
	getRqst
} from '@/common/request.js'
import store from '@/store/index.js'
import websocket from '@/common/websocket.js'
export default {
	namespaced: true,
	state: {
		messages: [],
		status: {

		},
		group: {

		},
		currentId: ""
	},
	mutations: {
		clearAll: (state,payload)=>{
			state.messages = []
			state.status = {}
			state.group = {}
			state.currentId = ""
		},
		addMessage: (state, payload) => {
			state.messages.unshift(payload)
			pub(`vuex_gchat_addMessage`, payload)
			if (!state.group[payload.group_id]) {
				store.dispatch({
					type: `gchat/getGroupInfo`,
					payload: {
						group_id: payload.group_id * 1
					}
				})
			}
		},
		removeGroup:(state,payload)=>{
			delete state.group[payload]
		},
		setCurrentState: (state, payload) => {
			state.currentId = payload
		},
		setGroupInfoNn: (state, payload) => {
			state.group[payload.id].name = payload.name
			state.group[payload.id].note = payload.note
		},
		setGroupInfoState: (state, payload) => {
			console.log(3421, payload)
			state.group[payload.id] = payload
		},
		setStatusLoading: (state, payload) => {
			state.status[payload.id].loading = true
		},
		setMoreMessageState: (state, payload) => {
			state.messages = state.messages.concat(payload.data)
			if (payload.data.length < 20) {
				state.status[payload.id].loading = false
				state.status[payload.id].finish = true
			} else {
				state.status[payload.id].loading = false
				state.status[payload.id].finish = false
			}
		},
		setGroupChatMessage: (state, payload) => {
			// state.group[payload.id] = payload.data.group
			console.log(12322123, payload)
			let finish = false
			if (payload.data.data.length < 20) {
				finish = true
			}
			let loading = false
			state.messages = state.messages.concat(payload.data.data)
			state.status[payload.id] = {
				finish,
				loading
			}
			console.log("0002", state.status)
		},
	},
	actions: {
		async getGroupInfo({
			commit
		}, {
			payload
		}) {
			const ret = await getRqst().get(`/chat/chat/getGroupInfo`, {
				group_id: payload.group_id * 1
			})
			console.log(441122, ret)
			if (ret.success) {
				commit(`setGroupInfoState`, ret.data)
			}
			payload.callback()
		},
		async addUserToGroup({
			commit
		}, {
			payload
		}) {
			const ret = await getRqst().post(`/chat/chat/addUserToGroup`, {
				group_id: payload.data.group_id * 1,
				users: payload.data.users
			})
			console.log(4422, ret)
			if (ret.success) {
				commit(`setGroupInfoState`, ret.data)
			}
			payload.callback()
		},
		async getGChatMessageInit({
			state,
			commit
		}, {
			payload
		}) {
			if (state.group[payload.id]) {
				console.log("0001", state.group)
				commit(`setCurrentState`, payload.id)
				return payload.callback()
			}
			store.dispatch({
				type: "gchat/getGroupInfo",
				payload: {
					group_id: payload.id,
					callback: async function() {
						const ret = await getRqst().get(`/chat/chat/gchat/${payload.id}`)
						if (ret.success) {
							commit('setGroupChatMessage', {
								id: payload.id,
								data: ret.data
							})
							payload.callback()
						}
					}
				},

			})

		},
		async exitGroup({
			state,
			commit
		}, {
			payload
		}) {
			console.log(333333, payload)
			payload.callback()
			setTimeout(() => {
				getRqst().post(`/chat/chat/exitGroup/${payload.group_id}/${store.state.user.user.id}`)
			}, 1000)
		},
		async deleteGroup({
			state,
			commit
		}, {
			payload
		}) {
			console.log(333333, payload)
			payload.callback()
			setTimeout(() => {
				getRqst().delete(`/chat/chat/deleteGroup/${payload.group_id}`)
			}, 1000)
		},
		async getMoreMessage({
			state,
			commit
		}, {
			payload
		}) {
			const ret = await getRqst().get(`/chat/chat/gmessage/${payload.id}`, {
				currentTime: payload.time,
				currentId: payload.currentId
			})
			if (ret.success) {
				commit('setMoreMessageState', {
					data: ret.data,
					id: payload.id
				})
				payload.callback()
			}
		},
		async setGroupInfo({
			state,
			commit
		}, {
			payload
		}) {
			console.log(12312331, payload)
			const ret = await getRqst().put(`/chat/chat/group/${payload.id}`, {
				name: payload.name,
				note: payload.note
			})
			console.log(12331, ret)
			if (ret.success) {
				commit('setGroupInfoNn', {
					name: payload.name,
					note: payload.note,
					id: payload.id
				})
				payload.callback()
			}
		},
	},
	init: function() {
		sub(`socket_gmessage`, function(e) {
			store.commit(`gchat/addMessage`, e)
		})
		sub(`socket_addUserToGroup`, function(e) {
			store.dispatch({
				type: `gchat/getGroupInfo`,
				payload: e
			})
			store.dispatch({
				type: `chats/init`,
				payload: {}
			})
		})
		sub(`socket_makeNewGroup`, function(e) {
			store.dispatch({
				type: `gchat/getGroupInfo`,
				payload: e
			})
			store.dispatch({
				type: `chats/init`,
				payload: {}
			})
		})

		sub(`socket_changeGroupInfo`, function(e) {
			console.log(55667, e)
			store.dispatch({
				type: `gchat/getGroupInfo`,
				payload: e
			})
			store.dispatch({
				type: `chats/init`,
				payload: {}
			})
		})

		sub(`socket_deleteGroup`, function(e) {
			console.log(55667, e)
			setTimeout(() => {
				store.dispatch({
					type: `gchat/getGroupInfo`,
					payload: e
				})
				store.dispatch({
					type: `chats/init`,
					payload: {}
				})
				if(store.state.chats.current == `g_${e.group_id}`){
					uni.switchTab({
						url: "/pages/index/index"
					})
				}
			},1200)
		})
		
		sub(`socket_exitGroup`, function(e) {
			console.log(55667, e)
			setTimeout(() => {
				if(e.user_id == store.state.user.user.id){
					store.commit(`gchat/removeGroup`,e.group_id)
				}else{
					store.dispatch({
						type: `gchat/getGroupInfo`,
						payload: e
					})
				}
				store.dispatch({
					type: `chats/init`,
					payload: {}
				})
				if(store.state.chats.current == `g_${e.group_id}`){
					uni.switchTab({
						url: "/pages/index/index"
					})
				}
			},1200)
		})
	
		sub("logout",function(){
			store.commit("gchat/clearAll", {})
		})
	}
}